
/******************************************************************************
 *
 * jattbak - TODO
 *
 * Author:        Dennis Krzyzaniak
 * E-mail:        ebrosius@netgarage.org
 * Copyright (C): 2011 by Dennis Krzyzaniak
 *
 * This file is part of jattbak.
 *
 * jattbak is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * jattbak is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with jattbak.  If not, see <http://www.gnu.org/licenses/>.
 *
 *****************************************************************************/

/* =========== INCLUDES ==================================================== */

#ifdef HAVE_CONFIG_H
#include "config.h"
#endif

#include "base_dlist.h"

#include <stdlib.h>
#include <assert.h>

/* =========== MODULE CONFIGURATION ======================================== */

/* =========== DEFINES ===================================================== */

/* =========== DATA TYPES ================================================== */

/* =========== GLOBALS ===================================================== */

/* =========== PRIVATE PROTOTYPES ========================================== */

/* =========== PUBLIC FUNCTIONS ============================================ */

/*---------------------------------------------------------------------------*/
void
baseDlistInit(
      baseDlist_t* const list
      )
{
   assert(list != NULL);

   list->head = (baseDlistNode_t*)&list->tail;
   list->tail = NULL;
   list->tailPred = (baseDlistNode_t*)&list->head;
}
/*---------------------------------------------------------------------------*/

/*---------------------------------------------------------------------------*/
void
baseDlistAddHead(
      baseDlist_t* const list,
      baseDlistNode_t* const node
      )
{
   assert(list != NULL);
   assert(node != NULL);

   node->successor = list->head;
   node->ancestor = (baseDlistNode_t*)&list->head;
   list->head->ancestor = node;
   list->head = node;
}
/*---------------------------------------------------------------------------*/

/*---------------------------------------------------------------------------*/
void
baseDlistAddTail(
      baseDlist_t* const list,
      baseDlistNode_t* const node
      )
{
   assert(list != NULL);
   assert(node != NULL);

   node->successor = (baseDlistNode_t*)&list->tail;
   node->ancestor = list->tailPred;
   list->tailPred->successor = node;
   list->tailPred = node;
}
/*---------------------------------------------------------------------------*/

/*---------------------------------------------------------------------------*/
void
baseDlistInsertBefore(
      baseDlist_t* const list,
      baseDlistNode_t* const successor,
      baseDlistNode_t* const node
      )
{
   assert(list != NULL);
   assert(node != NULL);

   if (successor == NULL || successor->ancestor == NULL) {
      baseDlistAddHead(list, node);
   } else {
      successor->ancestor->successor = node;
      node->ancestor = successor->ancestor;
      successor->ancestor = node;
      node->successor = successor;
   }
}
/*---------------------------------------------------------------------------*/

/*---------------------------------------------------------------------------*/
void
baseDlistInsertAfter(
      baseDlist_t* const list,
      baseDlistNode_t* const ancestor,
      baseDlistNode_t* const node
      )
{
   assert(list != NULL);
   assert(node != NULL);

   if (ancestor == NULL) {
      baseDlistAddHead(list, node);
   } else if (ancestor->successor == NULL) {
      baseDlistAddTail(list, node);
   } else {
      ancestor->successor->ancestor = node;
      node->successor = ancestor->successor;
      ancestor->successor = node;
      node->ancestor = ancestor;
   }
}
/*---------------------------------------------------------------------------*/

/*---------------------------------------------------------------------------*/
baseDlistNode_t*
baseDlistRemove(
      baseDlistNode_t* const node
      )
{
   assert(node != NULL);

   node->successor->ancestor = node->ancestor;
   node->ancestor->successor = node->successor;

   return node;
}
/*---------------------------------------------------------------------------*/

/*---------------------------------------------------------------------------*/
baseDlistNode_t*
baseDlistRemoveHead(
      baseDlist_t* const list
      )
{
   baseDlistNode_t* node;
   baseDlistNode_t* nodeSuccessor;

   assert(list != NULL);

   node = list->head;
   nodeSuccessor = node->successor;

   if (nodeSuccessor != NULL) {
      nodeSuccessor->ancestor = node->ancestor;
      list->head = nodeSuccessor;
      return node;
   }
   return NULL;
}
/*---------------------------------------------------------------------------*/

/*---------------------------------------------------------------------------*/
int
baseDlistGetItemCount(
      const baseDlist_t* const list
      )
{
   int count = 0;
   baseDlistNode_t* node;
   baseDlistNode_t* nodeSuccessor;

   assert(list != NULL);

   node = list->head;
   nodeSuccessor = node->successor;
   while (nodeSuccessor != NULL) {
      count++;
      node = nodeSuccessor;
      nodeSuccessor = node->successor;
   }

   return count;
}
/*---------------------------------------------------------------------------*/

/* =========== PRIVATE FUNCTIONS =========================================== */

/* ========================== END OF FILE ================================== */

/*
 * vim settings, please do not remove!
 * vim:autoindent:
 * vim:ts=3:sw=3:sts=3:expandtab:cindent:tw=75:formatoptions=croql:
 * vim600:foldmethod=syntax:
 */
